#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
视觉模型训练软件主程序入口
"""

import os
import sys
import yaml
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QTranslator, QLocale

from core.utils.logger import setup_logger
from gui.windows.main_window import MainWindow

def load_config():
    """加载配置文件"""
    config_path = os.path.join(os.path.dirname(__file__), "configs", "default.yaml")
    with open(config_path, 'r', encoding='utf-8') as f:
        return yaml.safe_load(f)

def setup_directories(config):
    """确保必要的目录存在"""
    for key, path in config['paths'].items():
        # 转换相对路径为绝对路径
        if not os.path.isabs(path):
            path = os.path.join(os.path.dirname(__file__), path)
            config['paths'][key] = path
            
        # 创建目录
        if not os.path.exists(path):
            os.makedirs(path, exist_ok=True)
    
    return config

def main():
    """主程序入口"""
    # 加载并设置配置
    config = load_config()
    config = setup_directories(config)
    
    # 设置日志系统
    log_path = config['paths']['logs']
    logger = setup_logger(log_path)
    logger.info("应用启动")
    
    # 创建应用
    app = QApplication(sys.argv)
    app.setApplicationName(config['app']['name'])
    app.setApplicationVersion(config['app']['version'])
    
    # 设置翻译器
    translator = QTranslator()
    locale = QLocale(config['app']['language'])
    QLocale.setDefault(locale)
    
    # 创建主窗口
    window = MainWindow(config, logger)
    window.show()
    
    # 运行应用
    sys.exit(app.exec_())

if __name__ == "__main__":
    main() 